<?php

defined('SYSPATH') or die('Access error!');

class Model_Q2a_Post extends Model
{

    public static $setting = array();

    public static function factory($table, $db = NULL)
    {
        self::$setting = Model_Setting::factory('setting')->get_all_setting();
        $class = 'Model_' . $table;
        return new $class($db, 'q2a_post');
    }

    /**
     *  获取给定条件的问答数量
     * @param string $where
     * @return type
     */
    public function get_count_post($where = '')
    {
        $where = ' 1=1 ' . $where;
        $sql = "SELECT COUNT(id) as num from " . $this->table . " WHERE " . $where;
        $total = $this->db->get_var($sql);
        return $total;
    }

    public function insert_post($data)
    {
        return $this->insert($data);
    }

    /**
     *  获取某一个问答的所属者的详细信息
     * @param type $id
     * @return type
     */
    public function get_post_user($id = '')
    {
        $sql = "SELECT u.username,u.id" .
                " FROM " . $this->table . " AS p " .
                "LEFT JOIN " . $this->db->get_table('user') . " AS u ON p.user_id = u.id" .
                " WHERE  p.id = " . $id;
        return (array) $this->db->get_row($sql, ARRAY_A);
    }

    /**
     * 根据问答的id获取详细信息
     * @param type $id
     * @return type
     */
    public function get_post_by_id($id)
    {
        $sql = 'SELECT p.*,u.username,u.id as user_id,u.avatar,c.name as category_name ,c.id as category_id ' .
                'FROM ' . $this->table . ' AS p ' .
                'LEFT JOIN ' . $this->db->get_table('user') . ' AS u ON u.id = p.user_id ' .
                'LEFT JOIN ' . $this->db->get_table('category') . ' AS c ON c.id = p.category_id ' .
                "WHERE p.id = " . $id;
        $post_detail = $this->db->get_row($sql, ARRAY_A);

        if (Arr::validate_arr($post_detail))
        {
            $post_detail['avatar_path'] = Model::factory('user')->create_avatar_path($post_detail['user_id'], $post_detail['avatar']);
        }
        return $post_detail;
    }

    /**
     *  获取某个用户相关的问题
     * @param type $user_id
     * @param type $type
     * @return type
     */
    public function get_user_related_post($user_id, $type)
    {
        $data = array();
        $data['user_id'] = $user_id;
        $data['post_type'] = $type;
        return $this->get_post_list_limit($data, self::$setting['q2a_user_related_num']);
    }

    /**
     * 获取某个分类的相关问题
     * @param type $category_id
     * @param type $type
     * @return type
     */
    public function get_category_related_post($category_id, $type)
    {
        $data = array();
        $data['category_id'] = $category_id;
        $data['post_type'] = $type;
        return $this->get_post_list_limit($data, self::$setting['q2a_similar_num']);
    }

    /**
     * 服务于别的方法主要是用来连接数量
     * @param type $data_where
     * @param type $limit
     * @return type
     */
    public function get_post_list_limit($data_where, $limit)
    {
        $where = ' AND ';
        if (is_array($data_where) && count($data_where) > 0)
        {
            $where .= $this->get_where($data_where);
        }
        $where .=
                " ORDER BY position,id DESC" .
                " LIMIT " . $limit;
        $post_arr = $this->get_results($where, 'id,name,view_count,answer_count,add_time');
        return $post_arr;
    }

    /**
     *  增加浏览次数
     * @param type $id
     * @param type $view_count
     */
    public function add_view_count($id, $view_count)
    {
        if (!empty($id))
        {
            $this->update(array('view_count' => $view_count + 1), array('id' => $id));
        }
    }

    /**
     *  用于后台的问题post列表
     * @param type $where_sql
     * @return type
     */
    public function get_post_list($where_sql = '')
    {
        $setting = self::$setting;
        $records_page = isset($setting['q2a_show_list_num']) && !empty($setting['q2a_show_list_num']) ? $setting['q2a_show_list_num'] : 10;
        $where = '';
        if (Arr::validate_arr($where_sql))
        {
            $where .= $this->get_table_where('p', $where_sql);
        }
        else
        {
            $where .= $where_sql;
        }
        $where .= ' ORDER BY p.position,p.id DESC';
        $sql = "SELECT p.name,p.content,p.id,p.add_time,p.parent_id,p.view_count,p.points,p.up_votes,p.down_votes,p.comment_count,p.answer_count,u.username,u.avatar,u.id as user_id,u.username" .
                " FROM " . $this->table . " AS p" .
                " LEFT JOIN " . $this->db->get_table('user') . " AS u ON u.id=p.user_id" . $where;
        $temp = $this->get_page_list($sql, $records_page);
        $data = array();
        if (Arr::validate_arr($temp['list']))
        {
            foreach ($temp['list'] as $value)
            {
                $value['url'] = $this->create_question_detail_url($value['id']);
                $data[] = $value;
            }
        }
        $temp['list'] = $data;
        return $temp;
    }

    /**
     *  生成问题详细页面的连接
     * @param type $id
     * @return type
     */
    public function create_question_detail_url($id)
    {
        return URL::site('question/detail/' . $id);
    }

    /**
     *  生成问题列表的连接
     * @param type $category_id
     * @return type
     */
    public function create_question_list_url($category_id)
    {
        return URL::site('question/list/' . $category_id);
    }

    /**
     *  删除某一个问题  包含所有的回复和回复的评论
     * @param type $id
     * @return boolean
     */
    public function delete_post($id)
    {
        //获取问题的所有回复
        $answer_list = $this->get_col(array('parent_id' => $id), 'id');
        //删除所有的回复
        $delete_answer_sql = "DELETE FROM " . $this->table . " WHERE post_type='answer' AND " . $this->get_in($answer_list, 'id');
        //获取所有回复的评论id
        $comment_list = $this->get_col($this->get_in($answer_list, 'parent_id'), 'id');
        //删除所有回复的评论
        $delete_comment_sql = "DELETE FROM " . $this->table . " WHERE post_type='comment' AND " . $this->get_in($comment_list, 'id');

        //你顺序删除
        $this->db->query($delete_comment_sql);
        $this->db->query($delete_answer_sql);
        //删除问题本身
        $this->delete(array('id' => $id));
        return TRUE;
    }

}